package com.chuangke.admin.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuangke.admin.dao.mapper.SysLogMapper;
import com.chuangke.admin.entity.SysLog;
import com.chuangke.admin.service.SysLogService;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.common.utils.BusinessUtil;

@Service
public class SysLogServiceImpl implements SysLogService {

	private static final Logger log = LoggerFactory.getLogger(SysLogServiceImpl.class);
	@Autowired
	private SysLogMapper sysLogMapper;

	@Async
	@Override
	public void save(SysLog record) {
		if (BusinessUtil.isInsert(record.getId())) {
			record.setId(IdManager.nextId());
			sysLogMapper.insert(record);
		} else {
			sysLogMapper.update(record, new UpdateWrapper<SysLog>().lambda().eq(SysLog::getId, record.getId()));
		}
	}

	@Override
	public int delete(SysLog record) {
		return sysLogMapper.deleteById(record.getId());
	}

	@Override
	public int delete(List<SysLog> records) {
		for (SysLog record : records) {
			delete(record);
		}
		return 1;
	}

	@Override
	public SysLog findById(String id) {
		return sysLogMapper.selectById(id);
	}

	@Override
	public IPage<SysLog> findPage(CkPageQuery pageRequest) {
		String userName = (String) pageRequest.get("userName");
		QueryWrapper<SysLog> qw = new QueryWrapper<>() ;
		if(userName != null) {
			qw.lambda().like(SysLog::getUserName, userName) ;
		}
		String param = (String) pageRequest.get("param");
		if(StringUtils.isNotBlank(param)) {
			qw.lambda().like(SysLog::getParams, param) ;
		}
		String method = (String) pageRequest.get("method");
		if(StringUtils.isNotBlank(method)) {
			qw.lambda().like(SysLog::getMethod, method) ;
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String startTime = (String) pageRequest.get("startTime");
		if( StringUtils.isNotBlank(startTime)) {
			LocalDate startDateTime = LocalDate.parse(startTime);
			LocalDateTime startOfDay = startDateTime.atStartOfDay();
			startTime = startOfDay.format(formatter);
			qw.lambda().ge(SysLog::getCreatedAt, startTime);
		}
		String endTime = (String) pageRequest.get("endTime");
		if(StringUtils.isNotBlank(endTime)) {
			LocalDate endDateTime = LocalDate.parse(endTime);
			LocalDateTime endOfDay = endDateTime.atTime(23, 59, 59);
			endTime = endOfDay.format(formatter);
			qw.lambda().le(SysLog::getCreatedAt, endTime);
		}
		qw.lambda().orderByDesc(SysLog::getCreatedAt) ;
		return sysLogMapper.selectPage(pageRequest.getPage(), qw) ;
	}

}
